package HWLayer;
import java.io.*;
import gnu.io.*;

public class RS232Display
{
    String selectedPort;
    SerialPort serialPort;
    OutputStream outputStream;

    public RS232Display(String selectedPort) {
        this.selectedPort = selectedPort;
    }

    private static String getPortTypeName ( int portType ) {
        switch ( portType )
        {
            case CommPortIdentifier.PORT_I2C:
                return "I2C";
            case CommPortIdentifier.PORT_PARALLEL:
                return "Parallel";
            case CommPortIdentifier.PORT_RAW:
                return "Raw";
            case CommPortIdentifier.PORT_RS485:
                return "RS485";
            case CommPortIdentifier.PORT_SERIAL:
                return "Serial";
            default:
                return "unknown type";
        }
    }

    private static void listPorts() {
        java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
        while ( portEnum.hasMoreElements() )
        {
            CommPortIdentifier portIdentifier = portEnum.nextElement();
            System.out.println(portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType()) );
        }
    }

    private void sendCommand(String sendString) {
        boolean portFound = false;

        java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
        while ( portEnum.hasMoreElements() )
        {
            CommPortIdentifier portIdentifier = portEnum.nextElement();
            if ((portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) && (portIdentifier.getName().equals(selectedPort))) {
                portFound = true;
                try {
                    serialPort = (SerialPort) portIdentifier.open("SimpleWrite", 2000);
                } catch (PortInUseException e) {
                    System.out.println("Port in use.");
                    continue;
                }

                try {
                    outputStream = serialPort.getOutputStream();
                } catch (IOException e) {}

                try {
                    serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                } catch (UnsupportedCommOperationException e) {}

                try {
                    serialPort.notifyOnOutputEmpty(true);
                } catch (Exception e) {
                    System.out.println("Error setting event notification");
                    System.out.println(e.toString());
                    System.exit(-1);
                }

                try {
                    outputStream.write(0x02);
                    outputStream.write(sendString.getBytes());
                    outputStream.write(0x03);
                } catch (IOException e) {}

                try {
                    Thread.sleep(50);  // Be sure data is xferred before closing
                } catch (Exception e) {}
                serialPort.close();
            }
        }

        if (!portFound) {
            System.out.println("port " + selectedPort + " not found.");
        }
    }

    public void sendClearDisplay() {
        sendCommand("<cle>");
    }

    public void sendPullRelay() {
        sendCommand("<rel>");
    }

    public void sendWriteLine(int line, String string) {
        sendCommand("<li" + line + ">" + string);
    }

    public void injectText() {
        sendCommand("<cle>");
        sendCommand("<li1>Recieving Packet..");
        sendCommand("<li2>Processing Data..");
        sendCommand("<li3>Writing Output..");
        sendCommand("<li4>Closing..");
    }
}
